package com.shenglue.ssi.action;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.shenglue.ssi.domain.BuyBackMain;
import com.shenglue.ssi.domain.Exportable;
import com.shenglue.ssi.domain.Supp;
import com.shenglue.ssi.service.BuyBackMainService;

public class BuyBackMainAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private BuyBackMain buyBackMain;	// 对应的 Bean
	private int buyBackMainId;		// 删除/修改 的 记录之Id
	private BuyBackMainService buyBackMainService; // 依赖的 Service

	/**
	 * 查询所有入库退货单主表信息方法
	 */
	public String queryAll() throws Exception {
		List<BuyBackMain> list = buyBackMainService.selectAllBuyBackMain();
		if( list != null ) {
			HttpServletRequest request = ServletActionContext.getRequest();
			request.setAttribute( "buyBackMainList", list );
		}else{
			// 不能认为是错误，因为一开始是没有数据的  return INPUT;
		}

		boolean flag = queryAllSupp( 1 );
		if( flag == false ) {
			return ERROR;
		}

		return SUCCESS;
	}

	/**
	 * 按条件查询 入库退货单主表信息方法
	 */
	public String selectBuyBackMainByCond() throws Exception {
		List<BuyBackMain> list = buyBackMainService.selectBuyBackMainByCond ( buyBackMain );
		if( list != null ) {
			HttpServletRequest request = ServletActionContext.getRequest();
			request.setAttribute( "buyBackMainList", list );
		}else{
			// 不能认为是错误，因为一开始是没有数据的  return INPUT;
		}

		boolean flag = queryAllSupp( 1 );
		if( flag == false ) {
			return ERROR;
		}

		return SUCCESS;
	}

	/**
	 * 增加入库退货单主表记录方法
	 */
	public String addNew() throws Exception {
		boolean flag = buyBackMainService.saveBuyBackMain( buyBackMain );
		if(flag==true){
			return SUCCESS;
		}else{
			return ERROR;
		}
	}

	/**
	 * 删除入库退货单主表记录方法
	 */
	public String delete() throws Exception {

		boolean flag = buyBackMainService.deleteBuyBackMain( buyBackMainId );
		if( flag == true ) {
			return SUCCESS;
		}else{
			return ERROR;
		}
	}

	/**
	 * 修改入库退货单主表记录方法
	 */
	public String update() throws Exception {

		boolean flag = buyBackMainService.updateBuyBackMain( buyBackMain );
		if( flag == true ) {
			return SUCCESS;
		}else{
			return ERROR;
		}
	}


	/**
	 * 准备 转向 新增 入库退货单主表记录 的界面
	 */
	public String prepareAddBuyBackMain() {
		boolean flag = queryAllSupp( 0 );
		if( flag == true ) {
			return SUCCESS;
		}else{
			return ERROR;
		}
	}

	/**
	 * 准备 转向 修改 入库退货单主表记录 的界面
	 */
	public String prepareModBuyBackMain() {
		boolean flag = queryAllSupp( 0 );
		if( flag == false ) {
			return ERROR;
		}

		buyBackMain = buyBackMainService.getOneBuyBackMainForUpdate( buyBackMainId );
		if( buyBackMain == null ) {
			return ERROR;
		}

		return SUCCESS;
	}



	/**
	 * 预先查好 供应商，为了制作下拉列表框
	 * @param workMode  0: 普通查询 ； 1: 在list的最前面，加一个空对象，以允许用户在选择时，不选 任何供应商
	 * @return
	 */
	private boolean queryAllSupp( int workMode ) {
		boolean bRtn;
		List<Supp> qrySupp = buyBackMainService.selectAllSupp();
		if( qrySupp != null ){
			List<Supp> reqSupp = new ArrayList<Supp>(); // 最终放入 request 的
			if ( workMode == 1 ) {
				reqSupp.add( new Supp() );
				reqSupp.addAll( qrySupp );
			} else {
				reqSupp = qrySupp;
			}
			HttpServletRequest request = ServletActionContext.getRequest();
			request.setAttribute( "allSupp", reqSupp );
			bRtn = true;
		}else{
			bRtn = false;
		}
		return bRtn;
	}

	/**
	 * 查询 入库退货单主表，并导出到 Excel
	 */
	public String queryAndExportExcel() throws Exception {
		fileName = "BuyBackMain__入库退货单信息";

		HttpServletRequest request = ServletActionContext.getRequest();

		List list = buyBackMainService.selectAllBuyBackMain(); // 此处 无法使用 泛型。 BuyBackMain、Exportable 都不行
		if( list == null ) {
			request.setAttribute( "message", "没有数据" );
			return ERROR;
		}

		HSSFWorkbook workbook = getWorkbook( list ); // 此处 无法使用 泛型。 BuyBackMain、Exportable 都不行

		if( workbook == null ) {
			request.setAttribute("message", "创建Excel失败");
			return ERROR;
		}

		try{

			//第四步：将工作簿写入最上面定义的InputStream流——名称为excelStream，这个名字对应struts.xml中配置的inputName参数
			this.workbook2InputStream( workbook, fileName );
			return SUCCESS;
		}catch(IOException e){
			e.printStackTrace();
			request.setAttribute("message", "创建Excel失败");
			return ERROR;
		}

	}

	public BuyBackMainAction() {
	}

	public BuyBackMain getBuyBackMain() {
		return buyBackMain;
	}

	public void setBuyBackMain(BuyBackMain buyBackMain) {
		this.buyBackMain = buyBackMain;
		// String handler = buyBackMain.getHandler();
		// String rightHandler;
		// try {
		// 	rightHandler = new String( handler.getBytes("iso-8859-1"), "utf-8" );
		// } catch (UnsupportedEncodingException e) {
		// 	rightHandler = "";
		// 	e.printStackTrace();
		// }
		// this.buyBackMain.setHandler( rightHandler );
	}

	public int getBuyBackMainId() {
		return buyBackMainId;
	}

	public void setBuyBackMainId(int buyBackMainId) {
		this.buyBackMainId = buyBackMainId;
	}

	public BuyBackMainService getBuyBackMainService() {
		return buyBackMainService;
	}

	public void setBuyBackMainService(BuyBackMainService buyBackMainService) {
		this.buyBackMainService = buyBackMainService;
	}

}
